#!/usr/bin/env php
<?php
require 'PWC/Common.class.php';
require 'PWC/Agent.class.php';
require 'Shell/phplib.inc';

$params = array('config'=>'config|c');
$desc   = array('config'=>'Configuration file in XML format');
$args   = Shell\arguments($argv,$params);

if(empty($args['config']) || !file_exists($args['config'])){
	echo "No config file.\n";
	exit (Shell\showhelp($params,$desc));
}

$config_file = $args['config'];
try{
    $agent_obj = new PWC\Agent($config_file); 
    $agent_obj->initialize($config_file);
}catch(Exception $e){
    exit($e->getMessage());
}

$modified_time = filemtime($config_file);
$start_time    = microtime(1);
echo "Start time:".date('Y-m-d H:i:s',$start_time);
do
{
    $urls = $agent_obj->getUrls();
    if(empty($urls)) { // 如果还有 processor 在处理，则等待所有处理完成
        echo '.';
        usleep($agent_obj->getConfig()->wait_usleep_time);
        continue;
    }

    foreach($urls as $row) { // 获得RUL内容，如果所有连接已满，则等所连接释放
        $wait_time = 0;
        $agent_obj->getContents($row, $wait_time);
        if($wait_time) {
            echo '.';
            usleep($wait_time);
        }
    }

    if($modified_time != filemtime($config_file)) {  // reload config file
        try{
            $agent_obj->initialize($config_file);
        }catch(Exception $e){
            exit($e->getMessage());
        }
    }
}
while($agent_obj->isProcessing());

$agent_obj->remove();
$end_time = microtime(1);
printf("\nEnd time:%s\nTotal processed %s URLs, Total time %s seconds\n",
    date('Y-m-d H:i:s',$end_time),
    $agent_obj->getTotalNumber(),
    sprintf('%.4f',$end_time-$start_time)
);

